﻿using System.Text;
using Sanelib.Common.SystemSettings;

namespace Sanelib.CodeMaker.Generators
{
    public class ControllerApiGenerator
    {
        public static void Generate(DatabaseTableSchema tbSchema)
        {
            var sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using AutoMapper;");
            sb.AppendLine("using WebApp.Services;");
            sb.AppendLine("using Common.Commands." + tbSchema.TableName + "Commands;");
            sb.AppendLine("using Dto.ApiRequests;");
            sb.AppendLine("using System.Net.Http;");

            sb.AppendLine("");
            sb.AppendLine("namespace WebApp.ControllerApis");
            sb.AppendLine("{");
            sb.AppendLine("\tpublic class " + tbSchema.TableName + "Controller : SmartApiController");
            sb.AppendLine("\t{");
            sb.AppendLine("\t\tpublic " + tbSchema.TableName + "Controller(IUserSession userSession, IMappingEngine mappingEngine) : base(userSession, mappingEngine)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t}");
            sb.AppendLine("");

            sb.AppendLine("\t\tpublic HttpResponseMessage Post(Add" + tbSchema.TableName + "Form form)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tvar command = Mapper.Map<Add" + tbSchema.TableName + "Form, Add" + tbSchema.TableName + ">(form);");
            sb.AppendLine("\t\t\treturn ExecuteCommand(command);");
            sb.AppendLine("\t\t}");
            sb.AppendLine("");

            sb.AppendLine("\t\tpublic HttpResponseMessage Put(Update" + tbSchema.TableName + "Form form)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tvar command = Mapper.Map<Update" + tbSchema.TableName + "Form, Update" + tbSchema.TableName + ">(form);");
            sb.AppendLine("\t\t\treturn ExecuteCommand(command);");
            sb.AppendLine("\t\t}");
            sb.AppendLine("");

            sb.AppendLine("\t\tpublic HttpResponseMessage Delete(Guid id)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tvar command = new Delete" + tbSchema.TableName + "(id);");
            sb.AppendLine("\t\t\treturn ExecuteCommand(command);");
            sb.AppendLine("\t\t}");


            sb.AppendLine("\t}");
            sb.AppendLine("}");
            
            System.IO.File.WriteAllText(Globals.CreateOrGetCustomPath("\\ControllerApis\\") + tbSchema.TableName + "Controller_Generated.cs", sb.ToString());
        }
    }
}
